﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Songhay.Silverlight.BiggestBox.IndexPart.Models
{
    using ViewModels;

    /// <summary>
    /// Master-Detail Data
    /// </summary>
    public static class MasterDetailData
    {
        /// <summary>
        /// Initializes the <see cref="MasterDetailData"/> class.
        /// </summary>
        static MasterDetailData()
        {
            MasterDetailData.isAscendingSortForPropertyOne = true;
        }

        /// <summary>
        /// Gets the collection count.
        /// </summary>
        /// <value>The collection count.</value>
        public static int CollectionCount
        {
            get { return (MasterDetailData.list != null) ? MasterDetailData.list.Count : 0; }
        }

        /// <summary>
        /// Loads these data.
        /// </summary>
        public static void Load()
        {
            MasterDetailData.list = MasterDetailData.GetSampleData();
        }

        /// <summary>
        /// Sorts the data.
        /// </summary>
        /// <param name="sortExpression">The sort expression.</param>
        public static void Sort(string sortExpression)
        {
            if(sortExpression == "PropertyOne")
            {
                if(MasterDetailData.isAscendingSortForPropertyOne)
                {
                    MasterDetailData.list = MasterDetailData.list.OrderBy(i => i.PropertyOne).ToList();
                }
                else
                {
                    MasterDetailData.list = MasterDetailData.list.OrderByDescending(i => i.PropertyOne).ToList();
                }
                MasterDetailData.isAscendingSortForPropertyOne = !MasterDetailData.isAscendingSortForPropertyOne;
            }
            else if(sortExpression == "PropertyOneDate")
            {
                if(MasterDetailData.isAscendingSortForPropertyOneDate)
                {
                    MasterDetailData.list = MasterDetailData.list.OrderBy(i => i.PropertyOneDate).ToList();
                }
                else
                {
                    MasterDetailData.list = MasterDetailData.list.OrderByDescending(i => i.PropertyOneDate).ToList();
                }
                MasterDetailData.isAscendingSortForPropertyOneDate = !MasterDetailData.isAscendingSortForPropertyOneDate;
            }
        }

        /// <summary>
        /// Deletes by the specified primary key.
        /// </summary>
        /// <param name="primaryKey">The primary key.</param>
        public static void Delete(int primaryKey)
        {
            var item = MasterDetailData.list.First(i => i.PrimaryKey == primaryKey);
            MasterDetailData.list.Remove(item);
        }

        /// <summary>
        /// Gets the next primary key.
        /// </summary>
        public static int? GetNextPrimaryKey()
        {
            var id = MasterDetailData.list.Max(i => i.PrimaryKey);
            return ++id;
        }

        /// <summary>
        /// Inserts with the specified arguments.
        /// </summary>
        /// <param name="isPropertyZero">if set to <c>true</c> [is property zero].</param>
        /// <param name="propertyOne">The property one.</param>
        /// <param name="propertyOneDate">The property one date.</param>
        public static void Insert(bool isPropertyZero, string propertyOne, DateTime propertyOneDate)
        {
            var id = MasterDetailData.list.Max(i => i.PrimaryKey);
            var item = new MasterDetailDatumViewModel();
            MasterDetailData.list.Add(item);
            item.PrimaryKey = (++id);
            item.IsPropertyZero = isPropertyZero;
            item.PropertyOne = propertyOne;
            item.PropertyOneDate = propertyOneDate;
        }

        /// <summary>
        /// Gets or sets the data items.
        /// </summary>
        /// <value>The data items.</value>
        public static IList<MasterDetailDatumViewModel> DataItems
        {
            get { return MasterDetailData.list; }
            set { MasterDetailData.list = value; }
        }

        /// <summary>
        /// Updates by the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static void Update(MasterDetailDatumViewModel item)
        {
            if(item == null) return;

            var search = MasterDetailData.list.FirstOrDefault(i => i.PrimaryKey == item.PrimaryKey);

            if(search == null)
            {
                //Insert:
                MasterDetailData.list.Add(item);
            }
            else
            {
                //Update:
                search.IsPropertyZero = item.IsPropertyZero;
                search.PropertyOne = item.PropertyOne;
                search.PropertyOneDate = item.PropertyOneDate;
            }
        }

        static IList<MasterDetailDatumViewModel> GetSampleData()
        {
            MasterDetailData.list = new List<MasterDetailDatumViewModel>();

            #region Add sample data:

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 1,
                PropertyOne = "My Property",
                PropertyOneDate = DateTime.Now
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 2,
                PropertyOne = "My Other Property",
                PropertyOneDate = DateTime.Now.AddDays(-13).AddMinutes(34)
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 3,
                IsPropertyZero = true,
                PropertyOne = "Yet Other Property",
                PropertyOneDate = DateTime.Now.AddDays(-3).AddMinutes(12),
                NestedDataItems = new List<MasterDetailDatumViewModel>
                {
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 31,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Three [1]",
                        PropertyOneDate = DateTime.Now.AddDays(-4).AddMinutes(1)
                    },
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 32,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Three [2]",
                        PropertyOneDate = DateTime.Now.AddDays(-3).AddMinutes(2)
                    },
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 33,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Three [3]",
                        PropertyOneDate = DateTime.Now.AddDays(-4).AddMinutes(3)
                    }
                }
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 4,
                PropertyOne = "My Property of the fourth",
                PropertyOneDate = DateTime.Now.AddDays(-9).AddMinutes(5)
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 5,
                PropertyOne = "Property Five",
                PropertyOneDate = DateTime.Now.AddDays(-3).AddMinutes(34)
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 6,
                IsPropertyZero = true,
                PropertyOne = "Property of Six",
                PropertyOneDate = DateTime.Now.AddDays(3).AddMinutes(64)
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 7,
                PropertyOne = "Seventh Property",
                PropertyOneDate = DateTime.Now
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 8,
                PropertyOne = "My Other Property, of Eight",
                PropertyOneDate = DateTime.Now.AddDays(-7).AddMinutes(54),
                NestedDataItems = new List<MasterDetailDatumViewModel>
                {
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 81,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Eight [1]",
                        PropertyOneDate = DateTime.Now.AddDays(-4).AddMinutes(1)
                    },
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 82,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Eight [2]",
                        PropertyOneDate = DateTime.Now.AddDays(-3).AddMinutes(2)
                    }
                }
            });

            MasterDetailData.list.Add(new MasterDetailDatumViewModel
            {
                PrimaryKey = 9,
                IsPropertyZero = true,
                PropertyOne = "Ninth Property",
                PropertyOneDate = DateTime.Now.AddDays(-8).AddMinutes(3),
                NestedDataItems = new List<MasterDetailDatumViewModel>
                {
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 91,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Nine [1]",
                        PropertyOneDate = DateTime.Now.AddDays(-4).AddMinutes(1)
                    },
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 92,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Nine [2]",
                        PropertyOneDate = DateTime.Now.AddDays(-3).AddMinutes(2)
                    },
                    new MasterDetailDatumViewModel
                    {
                        PrimaryKey = 93,
                        IsPropertyZero = true,
                        PropertyOne = "Nested Nine [3]",
                        PropertyOneDate = DateTime.Now.AddDays(-4).AddMinutes(3)
                    }
                }
            });

            #endregion

            return MasterDetailData.list;
        }

        static bool isAscendingSortForPropertyOne;
        static bool isAscendingSortForPropertyOneDate;
        static IList<MasterDetailDatumViewModel> list;
    }
}
